package com.cn.practice.adapter;

/**
 * 
 * @author 15790
 *
 */
public class AdapterTest {
	
public static void main(String[] args) {
	/**
	 * 场景实现 将任意电压转成5V给手机充电
	 */
	//new一个手机
	Phone phone = new Phone();
	//new一个5V充电头-类适配器
	Classadapter classadapter = new Classadapter();
	//将手机和充电头组装起来充电
//	phone.setChange220Vto5V(classadapter);
	phone.charge(classadapter);
	//在new一个对象适配器,由于是聚合所以需要初始化电源
	ObjectAdapter objectAdapter = new ObjectAdapter(new IVoltage());
	phone.charge(objectAdapter);
	//接口适配器
	InterfaceAdapterImpl interfaceAdapterImpl = new InterfaceAdapterImpl(new IVoltage());
	phone.charge(interfaceAdapterImpl);
}
}

/**
 * 电压
 * @author 15790
 *
 */
class IVoltage{
	private int voltage;
	//获取220V电压方法
	public int get220Voltage() {
		voltage=220;
		return voltage;
	}
	public int getVoltage() {
		return voltage;
	}
	public void setVoltage(int voltage) {
		this.voltage = voltage;
	}
	
}

/**
 * 适配接口 将220V电压转成5V
 * @author 15790
 *
 */
interface IVoltage220VChangeTo5V{
	int changeIVoltageTo5V();
}

/**
 * 类适配器
 * @author 15790
 *
 */
class Classadapter extends IVoltage implements IVoltage220VChangeTo5V{

	@Override
	public int changeIVoltageTo5V() {
		// 获取电压
		int voltage = this.get220Voltage();
		//将电压/44转电压
		int res=voltage/44;
		//将转出电压输出出去使用
		return res;
	}
	
}

/**
 * 对象适配器 不在继承src 而是聚合src
 * @author 15790
 *
 */
class ObjectAdapter implements IVoltage220VChangeTo5V{
	IVoltage voltage;
	
	
	public ObjectAdapter(IVoltage voltage) {
		super();
		this.voltage = voltage;
	}


	@Override
	public int changeIVoltageTo5V() {
		// TODO Auto-generated method stub
		int voltage2 = voltage.get220Voltage();
		//将电压/44转电压
		int res=voltage2/44;
		//将转出电压输出出去使用
		return res;
	}
	
}

/**
 * 接口适配器-接口适配器在springMVC就是用到了接口适配器
 * @author zhangyang
 *
 */
interface InterfaceAdapter extends IVoltage220VChangeTo5V{
	//适配规则
	void support();
	//适配后执行逻辑
	int handle();
}

abstract class AbsInterfaceAdapter implements InterfaceAdapter{

	IVoltage voltage;
	
	

	public AbsInterfaceAdapter(IVoltage voltage) {
		super();
		this.voltage = voltage;
	}

	@Override
	public void support() {
		// TODO Auto-generated method stub
		System.out.println("这里是匹配逻辑");
	}

	@Override
	public int handle() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int changeIVoltageTo5V() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
	
}

class InterfaceAdapterImpl extends AbsInterfaceAdapter{

	/**
	 * @param voltage
	 */
	public InterfaceAdapterImpl(IVoltage voltage) {
		super(voltage);
		// TODO Auto-generated constructor stub
	}

	@Override
	public int changeIVoltageTo5V() {
		// TODO Auto-generated method stub
		//将电压/44转电压
		int res=this.voltage.get220Voltage()/44;
		//将转出电压输出出去使用
		return res;
	}
	
	
	
}
/**
 * 手机-里面有一个充电方法
 * @author 15790
 *
 */
class Phone{
	IVoltage220VChangeTo5V change220Vto5V;
	
	public void charge(IVoltage220VChangeTo5V change220Vto5V) {
		int to5v = change220Vto5V.changeIVoltageTo5V();
		if(to5v==5) {
			System.out.println("使用类适配器转接成功，转为5V");
		}
	}

	public IVoltage220VChangeTo5V getChange220Vto5V() {
		return change220Vto5V;
	}


	public void setChange220Vto5V(IVoltage220VChangeTo5V change220Vto5V) {
		this.change220Vto5V = change220Vto5V;
	}
}